//
//  BKUseLoginFooterView.m
//  bestkeep
//
//  Created by windCould on 16/11/23.
//  Copyright © 2016年 utouu. All rights reserved.
//

#import "BKUseLoginFooterView.h"
#import "BKMyCommon.h"
#import <TencentOpenAPI/TencentOAuth.h>
#import "WXApi.h"
@interface BKUseLoginFooterView ()

@property (nonatomic,strong)UIButton * wechatBtn;
@property (nonatomic,strong)UIButton * qqBtn;
@property (nonatomic,strong)UILabel * weixinLab;
@property (nonatomic,strong)UILabel * qqLab;

@property (nonatomic,strong)UIView * leftView;
@property (nonatomic,strong)UIView * rightView;
@property (nonatomic,strong)UILabel * titleLabel;

@property (nonatomic,strong)UIButton * spaceBlankBtn;
@property (nonatomic,strong)UILabel * itemLab;
@property (nonatomic,assign)NSInteger  appNameValue;


@end


@implementation BKUseLoginFooterView


-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        [self setUpViews];
        self.appNameValue = [self isInstallApp];
    }
    
    return  self;
}

-(void)setAppNameValue:(NSInteger)appNameValue
{
    _appNameValue = appNameValue;
    if (_appNameValue == 12) {
        
        self.wechatBtn.hidden = NO;
        self.weixinLab.hidden = NO;
        self.qqBtn.hidden = NO;
        self.qqLab.hidden = NO;
        self.spaceBlankBtn.hidden = YES;
        self.itemLab.hidden = YES;
        self.hidden = NO;
    } else if (_appNameValue == 1){
        
        self.wechatBtn.hidden = YES;
        self.weixinLab.hidden = YES;
        self.qqBtn.hidden = YES;
        self.qqLab.hidden = YES;
        self.spaceBlankBtn.hidden = NO;
        self.itemLab.hidden = NO;
        
        [_spaceBlankBtn setBackgroundImage:[UIImage imageNamed:@"BK_My_logo_"] forState:UIControlStateNormal];
        self.itemLab.text = @"QQ登录";
        self.hidden = NO;
    } else if (_appNameValue == 3){
        
        self.wechatBtn.hidden = YES;
        self.weixinLab.hidden = YES;
        self.qqBtn.hidden = YES;
        self.qqLab.hidden = YES;
        self.spaceBlankBtn.hidden = NO;
        self.itemLab.hidden = NO;
        
        [_spaceBlankBtn setBackgroundImage:[UIImage imageNamed:@"BK_My_Wlogo_"] forState:UIControlStateNormal];
        self.itemLab.text = @"微信登录";
        self.hidden = NO;
    } else {
        
        self.hidden = YES;
        
    }
    
}

#pragma mark =========================点击事件================================

-(void)spaceBtnClick
{
    if ([self.myDelegate respondsToSelector:@selector(bkUseLoginFooterView:type:)]) {
        
        [self.myDelegate bkUseLoginFooterView:self type:_appNameValue];
    }
    
    
}

#pragma mark =========================布局===================================
-(void)setUpViews{
    
    
    _titleLabel =[[UILabel alloc]initWithFrame:CGRectZero];
    _titleLabel.font = [UIFont systemFontOfSize:14];
    _titleLabel.textColor= RGBCOLOR(64, 64, 64, 1);
    _titleLabel.text= @"第三方登录";
    _titleLabel.textAlignment = NSTextAlignmentCenter;
    [self addSubview:_titleLabel];
    
    _leftView = [[UIView alloc]initWithFrame:CGRectZero];
    _leftView.backgroundColor = RGBCOLOR(154, 154, 154, 1);
    [self addSubview:_leftView];
    
    _rightView = [[UIView alloc]initWithFrame:CGRectZero];
    _rightView.backgroundColor = RGBCOLOR(154, 154, 154, 1);
    [self addSubview:_rightView];
    
    _wechatBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [_wechatBtn setBackgroundImage:[UIImage imageNamed:@"BK_My_Wlogo_"] forState:UIControlStateNormal];
    [_wechatBtn addTarget:self action:@selector(wechatLogin) forControlEvents:UIControlEventTouchUpInside];
    _wechatBtn.hidden = YES;
    [self addSubview:_wechatBtn];
    
    _qqBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [_qqBtn setBackgroundImage:[UIImage imageNamed:@"BK_My_logo_"] forState:UIControlStateNormal];
    [_qqBtn addTarget:self action:@selector(qqLogin) forControlEvents:UIControlEventTouchUpInside];
    _qqBtn.hidden = YES;
    [self addSubview:_qqBtn];
    
    _weixinLab =[[UILabel alloc]initWithFrame:CGRectZero];
    _weixinLab.font = [UIFont systemFontOfSize:12];
    _weixinLab.textColor=RGBCOLOR(64, 64, 64, 1);
    _weixinLab.textAlignment = NSTextAlignmentCenter;
    _weixinLab.text=@"微信登录";
    _weixinLab.hidden = YES;
    [self addSubview:_weixinLab];
    
    _qqLab =[[UILabel alloc]initWithFrame:CGRectZero];
    _qqLab.font = [UIFont systemFontOfSize:12];
    _qqLab.textColor = RGBCOLOR(64, 64, 64, 1);
    _qqLab.textAlignment = NSTextAlignmentCenter;
    _qqLab.text=@"QQ登录";
    _qqLab.hidden = YES;
    [self addSubview:_qqLab];
    
    _spaceBlankBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    //[_spaceBlankBtn setBackgroundImage:[UIImage imageNamed:@"BK_My_Wlogo_"] forState:UIControlStateNormal];
    [_spaceBlankBtn addTarget:self action:@selector(spaceBtnClick) forControlEvents:UIControlEventTouchUpInside];
    _spaceBlankBtn.hidden = YES;
    [self addSubview:_spaceBlankBtn];
    
    _itemLab =[[UILabel alloc]initWithFrame:CGRectZero];
    _itemLab.font = [UIFont systemFontOfSize:12];
    _itemLab.textColor = RGBCOLOR(64, 64, 64, 1);
    _itemLab.textAlignment = NSTextAlignmentCenter;
    _itemLab.text=@"";
    _itemLab.hidden = YES;
    [self addSubview:_itemLab];
    
    [self layoutViews];
    
}

-(void)layoutViews{
    
    [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.mas_top).offset(0);
        make.centerX.mas_equalTo(self.centerX).offset(0);
        make.size.mas_equalTo(CGSizeMake(72, 20));
    }];
    
    [_leftView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.mas_top).offset(10);
        make.left.mas_equalTo(self.mas_left).offset(33);
        make.right.mas_equalTo(self.titleLabel.mas_left).offset(-10);
        make.height.mas_equalTo(@(0.5));
    }];
    
    [_rightView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.mas_top).offset(10);
        make.left.mas_equalTo(self.titleLabel.mas_right).offset(10);
        make.right.mas_equalTo(self.mas_right).offset(-33);
        make.height.mas_equalTo(@(0.5));
    }];
    
    [_wechatBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.leftView.mas_bottom).offset(24);
        make.right.mas_equalTo(self.titleLabel.mas_left).offset(0);
        make.size.mas_equalTo(CGSizeMake(33,33));
        
    }];
    
    [_qqBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.leftView.mas_bottom).offset(24);
        make.left.mas_equalTo(self.titleLabel.mas_right).offset(0);
        make.size.mas_equalTo(CGSizeMake(33,33));
        
    }];
    
    [_weixinLab mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.wechatBtn.mas_bottom).offset(10);
        make.centerX.mas_equalTo(self.wechatBtn.mas_centerX).offset(0);
        make.size.mas_equalTo(CGSizeMake(55, 12));
    }];
    
    [_qqLab mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.qqBtn.mas_bottom).offset(10);
        make.centerX.mas_equalTo(self.qqBtn.mas_centerX).offset(0);
        make.size.mas_equalTo(CGSizeMake(55, 12));
    }];
    
    
    [_spaceBlankBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.leftView.mas_bottom).offset(24);
        make.centerX.mas_equalTo(self.titleLabel.mas_centerX).offset(0);
        make.size.mas_equalTo(CGSizeMake(33,33));
    }];
    
    
    [_itemLab mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(_spaceBlankBtn.mas_bottom).offset(10);
        make.centerX.mas_equalTo(_spaceBlankBtn.mas_centerX).offset(0);
        make.size.mas_equalTo(CGSizeMake(55, 12));
    }];
    
}



//微信按钮
-(void)wechatLogin
{
    if ([self.myDelegate respondsToSelector:@selector(bkUseLoginFooterView: wechatLogin:)]){
        
        [self.myDelegate  bkUseLoginFooterView:self wechatLogin:nil];
    }
    
    
}

//qq按钮
-(void)qqLogin
{
    if ([self.myDelegate respondsToSelector:@selector(bkUseLoginFooterView: qqLogin:)]){
        
        [self.myDelegate bkUseLoginFooterView:self qqLogin:nil];
    }
    
}

#pragma mark =========================三方登录===================================

- (BOOL)isInChina {
    BOOL result = NO;
    
    NSLog(@"----->%@",[[NSTimeZone localTimeZone] name]);
    if([[[NSTimeZone localTimeZone] name] rangeOfString:@"Asia/Chongqing"].location == 0 ||
       [[[NSTimeZone localTimeZone] name] rangeOfString:@"Asia/Harbin"].location == 0 ||
       [[[NSTimeZone localTimeZone] name] rangeOfString:@"Asia/Hong_Kong"].location == 0 ||
       [[[NSTimeZone localTimeZone] name] rangeOfString:@"Asia/Macau"].location == 0 ||
       [[[NSTimeZone localTimeZone] name] rangeOfString:@"Asia/Shanghai"].location == 0 ||
       [[[NSTimeZone localTimeZone] name] rangeOfString:@"Asia/Taipei"].location == 0)
    {
        result = YES;
    }
    return result;
}

-(NSInteger)isInstallApp
{
    NSInteger  str = 0;
    if ([self isInChina]) {
        
        
        if ([TencentOAuth iphoneQQInstalled]) {
            
            str = 1;
        }
        
        if ([WXApi isWXAppInstalled]) {
            
            if (str == 1) {
                
                str = 12;
            } else {
                
                str = 3;
            }
        }
    }
    return str;
}






@end
